#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <config.h>
#include <citytab.h>
#include <weather.h>
#include <page_manager.h>
#include <photo_manager.h>

static int settingpageRun(void);
static int settingpageGetInputEvent(void);
static char *(*indexCity)[2];
static int allCityNum;
static int selectCityNum;
static int selectPrivenceNum;
static int indexPos;
static int touchNum=12;

static T_pageAction settingpage = 
{
	.name 			= "settingpage",
	.run  			= settingpageRun,
	.getInputEvent 	= settingpageGetInputEvent,
	.next 			= NULL,
};

static T_touchInfo settingPageIcon[]=
{
	{1150,0,150,0,"icon/cancel.bmp"},
	{1150,0,470,0,"icon/ok.bmp"},
	
	{30,300,70,130,"NULL"},
	{30,300,130,190,"NULL"},
	{30,300,190,250,"NULL"},
	{30,300,250,310,"NULL"},
	{30,300,310,370,"NULL"},
	{30,300,370,430,"NULL"},
	{30,300,430,490,"NULL"},
	{30,300,490,550,"NULL"},
	{30,300,550,610,"NULL"},
	{30,300,610,670,"NULL"},

	{310,570,70,130,"NULL"},
	{310,570,130,190,"NULL"},
	{310,570,190,250,"NULL"},
	{310,570,250,310,"NULL"},
	{310,570,310,370,"NULL"},
	{310,570,370,430,"NULL"},
	{310,570,430,490,"NULL"},
	{310,570,490,550,"NULL"},
	{310,570,550,610,"NULL"},
	{310,570,610,670,"NULL"},

	{580,840,70,130,"NULL"},
	{580,840,130,190,"NULL"},
	{580,840,190,250,"NULL"},
	{580,840,250,310,"NULL"},
	{580,840,310,370,"NULL"},
	{580,840,370,430,"NULL"},
	{580,840,430,490,"NULL"},
	{580,840,490,550,"NULL"},
	{580,840,550,610,"NULL"},
	{580,840,610,670,"NULL"},

	{850,1100,70,130,"NULL"},
	{850,1100,130,190,"NULL"},
	{850,1100,190,250,"NULL"},
	{850,1100,250,310,"NULL"},
	{850,1100,310,370,"NULL"},
	{850,1100,370,430,"NULL"},
	{850,1100,430,490,"NULL"},
	{850,1100,490,550,"NULL"},
	{850,1100,550,610,"NULL"},
	{850,1100,610,670,"NULL"},
};

static void showSettingPage(void)
{
	T_imageInfo imageData;
	int i;
	
	g_ptDisplayDevice->displayDeviceClear(0xffffff);
	freetypeShowStr(30, 0, 50, 3, "省/市", 0xff, 0xffffff);
	freetypeShowStr(310, 0, 50, 2, "城市", 0xff, 0xffffff);

	for(i=0;i<2;i++)
	{
		if(strstr(settingPageIcon[i].icon,".bmp")==0 || strstr(settingPageIcon[i].icon,".jpg")==0 || strstr(settingPageIcon[i].icon,".png")==0)
		{
			getPhotoData(settingPageIcon[i].icon, &imageData);
			settingPageIcon[i].xMax = settingPageIcon[i].xMin + imageData.width;
			settingPageIcon[i].yMax = settingPageIcon[i].yMin + imageData.height;
			showImage(settingPageIcon[i].xMin, settingPageIcon[i].yMin, &imageData);
		}
	}
}

static void showProvience(int pos)
{
	int num,i;
	PT_provinceST me = provinces;
	char provincesBuff[32];

	num = sizeof(settingPageIcon)/sizeof(settingPageIcon[0])-30;	
	if(pos<0)pos=0;
	if(pos>3)pos=3;
	if(pos==3)num=6;

	g_ptDisplayDevice->displayDeviceClearArea(0,60,300,LCDHEIGHT,0x87CEEB);
	g_ptDisplayDevice->displayDeviceClearArea(300,60,1100,LCDHEIGHT,0x03A89E);
	for(i=2;i<num;i++)
	{
		memset(provincesBuff,0,32);
		snprintf(provincesBuff,31,"%s\n",me[pos*10+i-2].provinceName);
		freetypeShowStr(settingPageIcon[i].xMin, settingPageIcon[i].yMin, 50, strlen(provincesBuff)/3, provincesBuff, 0xEB8E55, 0x87CEEB);
	}
}

static void showCity(int pos)
{
	PT_provinceST me = provinces;
	char cityBuff[32];
	int num,i;

	g_ptDisplayDevice->displayDeviceClearArea(300,60,1100,LCDHEIGHT,0x03A89E);
	if(pos>=34)pos = 33;
	indexCity = me[pos].provinceAddr;
	selectPrivenceNum = pos;

	num = sizeof(settingPageIcon)/sizeof(settingPageIcon[0]);
	allCityNum=0;
	for(i=12;i<num;i++)
	{
		memset(cityBuff,0,32);
		if(indexCity[i-12][0]==NULL) break;
		allCityNum++;
		snprintf(cityBuff,31,"%s\n",indexCity[i-12][0]);
		freetypeShowStr(settingPageIcon[i].xMin, settingPageIcon[i].yMin, 50, strlen(cityBuff)/3, cityBuff, 0xFFC0CB, 0x03A89E);
	}
	touchNum=i;
}

static int settingpageRun(void)
{
	int ret=0;
	
	showSettingPage();
	showProvience(0);

	while(1)
	{
		ret = settingpageGetInputEvent();
		if(ret >0)
		{
			switch(ret)
			{
				case 1:
					runPage("mainpage");
					break;
				case 2:
					setCurrentCity(indexCity[selectCityNum][1]);
					runPage("mainpage");
					break;
				case 3:
				case 4:
				case 5:
				case 6:
				case 7:
				case 8:
				case 9:
				case 10:
				case 11:
				case 12:
					showCity(indexPos*10+ret-3);
					break;
				case 13:
				case 14:
				case 15:
				case 16:
				case 17:
				case 18:
				case 19:
				case 20:
				case 21:
				case 22:
				case 23:
				case 24:
				case 25:
				case 26:
				case 27:
				case 28:
				case 29:
				case 30:
				case 31:
				case 32:
				case 33:
					selectCityNum=ret-13;
					if(selectCityNum>=allCityNum)selectCityNum = allCityNum-1;
					break;
				case 100:
					indexPos-=1;
					if(indexPos<0)indexPos=0;
					showProvience(indexPos);
					break;
				case 110:
					indexPos+=1;
					if(indexPos>3)indexPos=3;
					showProvience(indexPos);
					break;
				default:
					break;
			}	
		}
	}
	return 0;
}

static int settingpageGetInputEvent(void)
{
	T_inputEvent inputEvent;
	static char touchFlag=0;
	unsigned char *buff =NULL;
	unsigned int  bpp = g_ptDisplayDevice->pixel/8;
	int i,x,y;
	static int lastX=0;
	static int lastY=0;

	getInputEvent(&inputEvent);
	if(inputEvent.type == INPUT_TYPE_TOUCH)
	{
		if(inputEvent.pressed ==1 && touchFlag==0)
		{
			lastX = inputEvent.x;
			lastY = inputEvent.y;
			
			buff = g_ptDisplayDevice->getDisplayMmap();
			if(buff==NULL)
			{
				fprintf(stderr,"getDisplayMmap error\n");
				exit(1);
			}
			for(i=0;i<2;i++)
			{
				if((inputEvent.x>settingPageIcon[i].xMin) &&(inputEvent.x<settingPageIcon[i].xMax) && (inputEvent.y>settingPageIcon[i].yMin) &&(inputEvent.y<settingPageIcon[i].yMax))
				{
					for(y= settingPageIcon[i].yMin; y< settingPageIcon[i].yMax; y++)
					{
						for(x= settingPageIcon[i].xMin ; x< settingPageIcon[i].xMax; x++)
						{
							buff[y*g_ptDisplayDevice->lineSize +x*bpp] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp];
							buff[y*g_ptDisplayDevice->lineSize +x*bpp+1] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp+1];
							buff[y*g_ptDisplayDevice->lineSize +x*bpp+2] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp+2];
						}
					}
				}
			}

			touchFlag=1;
		}
		else if(inputEvent.pressed ==0 && touchFlag==1)
		{
			if((inputEvent.y-lastY)>120 && (inputEvent.x<300) &&(lastX<300))//向下
			{
				touchFlag=0;
				return 100;
			}
			else if((inputEvent.y-lastY)<-120 && (inputEvent.x<300) &&(lastX<300))//向上
			{
				touchFlag=0;
				return 110;
			}

			buff = g_ptDisplayDevice->getDisplayMmap();
			if(buff==NULL)
			{
				fprintf(stderr,"getDisplayMmap error\n");
				exit(1);
			}
			for(i=0;i<touchNum;i++)
			{
				if((inputEvent.x>settingPageIcon[i].xMin) &&(inputEvent.x<settingPageIcon[i].xMax) && (inputEvent.y>settingPageIcon[i].yMin) &&(inputEvent.y<settingPageIcon[i].yMax))
				{
					if(indexPos ==3 && i>5)
					{
						touchFlag=0;
						break;
					}
					if(i>=2 && i<12) 
					{
						showProvience(indexPos);
					}
					else if(i>=12)
					{
						showCity(selectPrivenceNum);
					}
					
					for(y= settingPageIcon[i].yMin; y< settingPageIcon[i].yMax; y++)
					{
						for(x= settingPageIcon[i].xMin ; x< settingPageIcon[i].xMax; x++)
						{
							buff[y*g_ptDisplayDevice->lineSize +x*bpp] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp];
							buff[y*g_ptDisplayDevice->lineSize +x*bpp+1] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp+1];
							buff[y*g_ptDisplayDevice->lineSize +x*bpp+2] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp+2];
						}
					}
					touchFlag=0;
					return i+1;
				}
			}
			touchFlag=0;
		}
	}

	return 0;
}

int settingpageInit(void)
{
	int ret;

	ret = registerPages(&settingpage);
	return ret;
}

